<%= javascript_include_tag "prototype" %>
<%= javascript_include_tag "util" %>
<%= javascript_include_tag "calendar" %>
<script language="javascript">
var oGoods=new Array();

function addProduct(){
        var url = '/sale/select_product';
        var l = oGoods.length;
        if (l > 0) {
                url += "?";
                for (var i=0; i < oGoods.length; i++) {
                        url += oGoods[i].serial + "=" + oGoods[i].total_counts + "&" ;
                }
                url = url.substring(0,url.length-1);
        }
        window.open(url,"selectProduct","menubar=no,toolbar=no,status=yes,scrollbars=yes,width=820,height=400");
}
function addGoods(sId,sSerial,sName,sTotal,sPrice,sBuy,sMemo,sF,sB,sP,sGoods_num){
        var newGoods=new Object();
        newGoods.id=sId;
        newGoods.serial=sSerial;
        newGoods.name=sName;
        newGoods.total_counts=parseFloat(sTotal);
        newGoods.sale_price=parseFloat(sPrice);
        newGoods.buy=parseFloat(sBuy);
        newGoods.memo=sMemo;
        newGoods.F=sF;
        newGoods.B=sB;
        newGoods.P=sP;
        newGoods.goods_num=sGoods_num;
        oGoods[oGoods.length]=newGoods;
        changeTable();
}
function checkPaperBox(e){
        if (!e){
                if ((event.keyCode<48 || event.keyCode>57) && event.keyCode!=8)
                        event.returnValue=false;
        }
        else{
                if ((e.which<48 || e.which>57) && e.which!=8)
                        return false;
        }
}
/*
function checkAmount(e){
        if (!e){
                if ((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 && event.keyCode!=8)
                        event.returnValue=false;
                else if(event.keyCode==46 && document.getElementById("salePaper_amount_of_insurance").value.search(/\./)!=-1)
                        event.returnValue=false;
        }
        else{
                if ((e.which<48 || e.which>57) && e.which!=46 && e.which!=8)
                        return false;
                else if(e.which==46 && document.getElementById("salePaper_amount_of_insurance").value.search(/\./)!=-1)
                        return false;
        }
        }
        */
function removeItems(index) {
        oGoods.splice(index,1);
        changeTable();
}
function changeTable(){
        var l=oGoods.length;
        var sHTML="",sPrice="";
        var lCount=0.000,lPrice=0.00;
        for (var i=0;i<l;i++){
                if (oGoods[i].sale_price<oGoods[i].buy)
                        sPrice='<td style="text-align: center;color: red;">'+oGoods[i].sale_price+'</td>';
                else
                        sPrice='<td style="text-align: center;">'+oGoods[i].sale_price+'</td>';
                sHTML	+='<tr id="LIST_TB_CONTENT1">'
                        + '<td style="text-align: center;">'+(1+i)+'</td>'
                        + '<td style="text-align: center;">'+oGoods[i].serial+'</td>'
                        + '<td style="text-align: center;">'+oGoods[i].name+'</td>'
                        + '<td style="text-align: center;">'+oGoods[i].total_counts+'</td>'
                        + '<td style="text-align: center;">'+oGoods[i].F+'</td>'
                        + '<td style="text-align: center;">'+oGoods[i].B+'</td>'
                        + '<td style="text-align: center;">'+oGoods[i].P+'</td>'
                        + sPrice
                        + '<td style="text-align: center;">'+oGoods[i].goods_num+'</td>'
                        + '<td style="text-align: center;">'+(parseFloat(oGoods[i].total_counts*oGoods[i].sale_price)).toFixed(2)+'</td>'
                        + '<td style="text-align: center;"><img src="/images/fail.gif" onclick="removeItems('+i+');"/></td>'
                        + '</tr>';
                lCount+=oGoods[i].total_counts;
                lPrice+=oGoods[i].total_counts*oGoods[i].sale_price;
        }
        $('divGoodsList').innerHTML	='<table  id="LIST_TB" style="width:100%">'
                +'<tbody>'
                +'<tr id="LIST_TB_CONTENT2">'
                +'<td style="text-align: center;">序号</td>'
                +'<td style="text-align: center;">品名规格</td>'
                +'<td style="text-align: center;">导针规格</td>'
                +'<td style="text-align: center;">数量（kpcs）</td>'
                +'<td style="text-align: center;">F</td>'
                +'<td style="text-align: center;">B</td>'
                +'<td style="text-align: center;">P</td>'
                +'<td style="text-align: center;">单价（元）</td>'
                +'<td style="text-align: center;">件数</td>'
                +'<td style="text-align: center;">小计（元）</td>'
                +'<td style="text-align: center;">删除</td>'
                +'</tr>'
                +sHTML
                +'<tr id="LIST_TB_CONTENT2">'
                +'<td style="text-align: center;">合计</td>'
                +'<td>&nbsp;</td>'
                +'<td>&nbsp;</td>'
                +'<td id=lCount style="text-align: center;">'+lCount.toFixed(3)+'</td>'
                +'<td>&nbsp;</td>'
                +'<td>&nbsp;</td>'
                +'<td>&nbsp;</td>'
                +'<td>&nbsp;</td>'
                +'<td>&nbsp;</td>'
                +'<td id=lPrice style="text-align: center;">'+lPrice.toFixed(2)+'</td>'
                +'<td>&nbsp;</td>'
                +'</tr>'
                +'</tbody>'
                +'</table>';

}
function showSelectBuyer(){
        window.open("/sale/select_buyer","selectProduct","menubar=no,toolbar=no,status=yes,scrollbars=yes,width=600,height=400");  
}
function selectAgent(id)
{
        var pars = 'id='+id;
        new Ajax.Request('/ajax/get_agent',{method:'get',parameters:pars,onComplete:addressResponse});
}
function addAgent(id,name,person,phone,address1,fax){
        $("salePaper_agent_id").value=id;
        $("agentName").innerHTML=name;
        $("id_person").value=person;
        $("id_phone").value=phone;
        $("id_address1").value=address1;
        $("id_fax").value=fax;
}
function addressResponse(response)
{
        p = response.responseXML.getElementsByTagName("phone")[0];
        $('id_phone').value = p.firstChild.nodeValue;
        p = response.responseXML.getElementsByTagName("person")[0];
        $('id_person').value = p.firstChild.nodeValue;
        p = response.responseXML.getElementsByTagName("address1")[0];
        $('id_address1').value = p.firstChild.nodeValue;
}

function formSubmit()
{
        //debugger;
        var sGoods="";
        var i,l=oGoods.length;
        if ($("salePaper_agent_id").value==""){
                alert("请先选择客户！");
                return;
        }
        if (l==0){
                alert("请先选择产品！");
                return;
        }
        for (i=0;i<l;i++) {
                sGoods+=oGoods[i].id+','+oGoods[i].total_counts+','+oGoods[i].sale_price+','+oGoods[i].memo+','+oGoods[i].goods_num+','+oGoods[i].F+','+oGoods[i].B+','+oGoods[i].P+';';
                //alert(sGoods);
        }

        $("hidProductInfo").value=sGoods;
        document.salePaper.submit();
}
</script>
<h1>订单管理 - <span id="secondHeading">生成订单</span></h1>
<div>
<% if @flash[:notice] -%>
<div id="notice"><%= @flash[:notice] %></div>
<% end -%>
</div>
<form action="/sale/add_sale_paper" method="post" name="salePaper">
<input type="hidden" id="hidProductInfo" name="hidProductInfo"/>
<input type="hidden" id="salePaper_agent_id" name="salePaper[agent_id]"/>
<table id="LIST_TB">
<tbody>
<tr id="LIST_TB_TITLE">
<td colspan="4" align="center">订货单</td>
</tr>
<tr id="LIST_TB_CONTENT1">
<td>购 买 方：</td>
<td><span id="agentName"></span>&nbsp;&nbsp;&nbsp;&nbsp;<input name="btnSelect" type="button" class='sub' value="选择" onclick="showSelectBuyer();"  /></td>
<td>客户订单号：</td>
<td><%= text_field "salePaper", "customer_book_no", :size => 22 %></td>
</tr>
<tr id="LIST_TB_CONTENT1">
<td>订单日期：</td>
<td colspan="4"><script language=javascript>
/*
   var bir_Date=new dateSelector();
   bir_Date.inputID='sale_date_at';
   bir_Date.inputName='salePaper[sale_date_at]';
   bir_Date.display();
 */
var dateTime = new Date();
document.write(dateTime.getYear() + "-" + (dateTime.getMonth()+1) + "-" + dateTime.getDate());
</script>   
</td>
<!--<td>交货日期：</td>
<td>
<script language=javascript>
var bir_Date=new dateSelector();
bir_Date.inputID='get_date_at';
bir_Date.inputName='salePaper[get_date_at]';
bir_Date.display();
</script>
      </td>-->
    </tr>
    <tr id="LIST_TB_CONTENT1">
            <td>联 系 人： </td>
            <td><input type="text" id="id_person" name="salePaper[agent_people]" value="" size="22" maxlength="20"/></td>
            <td>客户TEL：</td>
            <td><input type="text" id="id_phone" name="salePaper[agent_phone]" value="" size="22" maxlength="20"/></td>
    </tr>
    <tr id="LIST_TB_CONTENT1">
            <td>客户收货地址：</td>
            <td><input type="text" id="id_address1" name="salePaper[agent_address]" value="" size="30" maxlength="50"/></td>
            <td>客户FAX：</td>
            <td><input type="text" id="id_fax" name="salePaper[agent_fax]" value="" size="22" maxlength="20"/></td>
    </tr>
    <tr id="LIST_TB_CONTENT1">
            <td>销售方：</td>
            <td><input type="text" name="salePaper[sale_company]" value="<%=@myname.value%>" size="30" maxlength="30"/></td>
            <td>公司地址：</td>
            <td><input type="text" name="salePaper[sale_address]" value="<%=@myaddress.value%>" size="30" maxlength="50"/></td>
    </tr>
    <tr id="LIST_TB_CONTENT1">
            <td>公司电话：</td>
            <td><input type="text" name="salePaper[sale_phone]" value="<%=@myphone.value%>" size="22" maxlength="20"/></td>
            <td>公司传真:</td>
            <td><input type="text" name="salePaper[sale_fax]" value="<%=@myfax.value%>" size="22" maxlength="20"/></td>
    </tr>
    <tr id="LIST_TB_TITLE">
            <td colspan="4" align="center">订货明细</td>
    </tr>
    <tr>
            <td colspan="4" align="right">
                    <div id="divGoodsList" style="width:100%"></div>
                    <div><input type="button" value="添加产品" onclick="addProduct()" class="sub"/></div>
            </td>
    </tr>
    <!--
    <tr id="LIST_TB_CONTENT2">
            <td>发货单号:</td>
            <td><input type="text" name="salePaper[serial]" size="22" maxlength="20"/></td>
            <td>打包件数:</td>
            <td><input id="salePaperBox" type="text" name="salePaper[box_count]" size="22" maxlength="20"/></td>
    </tr>
    <tr id="LIST_TB_CONTENT2">
            <td>发货通知:</td>
            <td colspan="3"><%=text_area(:salePaper, :memo,:cols=>54,:rows=>10)%></td>
    </tr>
    <tr id="LIST_TB_CONTENT2">
            <td>是否支付全款:</td>
            <td colspan="3"><%=check_box("salePaper", "charge", {}, "yes", "no")%></td>
    </tr>
    -->
  </tbody>
</table>
<div style="margin-top: 10px;margin-left: 75%">
        <input type="button" value="添加"  onclick="formSubmit()" class="sub"/>&nbsp;OR&nbsp;<input type="reset" value="重新填写" class="cal"/>
</div>
</form>
<script type="text/javascript">
    //    document.getElementById("salePaperBox").on=checkPaperBox;
    //document.getElementById("salePaper_amount_of_insurance").onkeypress=checkAmount;
</script>
